home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / PGUIDE / PROGWOB / PWOSTRAW.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-11-26  |  8.0 KB  |  247 lines

  1. VERSION 5.00
  2. Begin VB.Form frmStraw 
  3.    Caption         =   "Employees Collection - House of Straw"
  4.    ClientHeight    =   3525
  5.    ClientLeft      =   1140
  6.    ClientTop       =   1515
  7.    ClientWidth     =   4995
  8.    LinkTopic       =   "Form1"
  9.    LockControls    =   -1  'True
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   3525
  12.    ScaleWidth      =   4995
  13.    WhatsThisHelp   =   -1  'True
  14.    Begin VB.CommandButton cmdTrouble 
  15.       Caption         =   "&Trouble"
  16.       Height          =   465
  17.       Left            =   3150
  18.       TabIndex        =   8
  19.       Top             =   2250
  20.       Width           =   1545
  21.    End
  22.    Begin VB.CommandButton cmdClose 
  23.       Caption         =   "&Close"
  24.       Height          =   285
  25.       Left            =   3150
  26.       TabIndex        =   9
  27.       Top             =   2880
  28.       Width           =   1545
  29.    End
  30.    Begin VB.CommandButton cmdListEmployees 
  31.       Caption         =   "&Refresh List"
  32.       Height          =   285
  33.       Left            =   3150
  34.       TabIndex        =   7
  35.       Top             =   1800
  36.       Width           =   1545
  37.    End
  38.    Begin VB.CommandButton cmdDeleteEmployee 
  39.       Caption         =   "&Delete"
  40.       Height          =   285
  41.       Left            =   3150
  42.       TabIndex        =   6
  43.       Top             =   1440
  44.       Width           =   1545
  45.    End
  46.    Begin VB.CommandButton cmdAddEmployee 
  47.       Caption         =   "&Add"
  48.       Default         =   -1  'True
  49.       Enabled         =   0   'False
  50.       Height          =   285
  51.       Left            =   3150
  52.       TabIndex        =   5
  53.       Top             =   1080
  54.       Width           =   1545
  55.    End
  56.    Begin VB.ListBox lstEmployees 
  57.       Height          =   1845
  58.       Left            =   180
  59.       Sorted          =   -1  'True
  60.       TabIndex        =   4
  61.       Top             =   1080
  62.       Width           =   2715
  63.    End
  64.    Begin VB.TextBox txtSalary 
  65.       Height          =   285
  66.       Left            =   2700
  67.       TabIndex        =   3
  68.       Top             =   450
  69.       Width           =   1995
  70.    End
  71.    Begin VB.TextBox txtName 
  72.       Height          =   285
  73.       Left            =   180
  74.       TabIndex        =   1
  75.       Top             =   450
  76.       Width           =   2265
  77.    End
  78.    Begin VB.Label Label2 
  79.       Caption         =   "&Salary"
  80.       Height          =   195
  81.       Left            =   2700
  82.       TabIndex        =   2
  83.       Top             =   180
  84.       Width           =   2025
  85.    End
  86.    Begin VB.Label Label1 
  87.       Caption         =   "&Name"
  88.       Height          =   195
  89.       Left            =   180
  90.       TabIndex        =   0
  91.       Top             =   180
  92.       Width           =   2265
  93.    End
  94. Attribute VB_Name = "frmStraw"
  95. Attribute VB_GlobalNameSpace = False
  96. Attribute VB_Creatable = False
  97. Attribute VB_PredeclaredId = True
  98. Attribute VB_Exposed = False
  99. Option Explicit
  100. Public sbMain As New SmallBusiness1
  101. Private Sub cmdAddEmployee_Click()
  102.     Dim empNew As New Employee
  103.     If Not IsNumeric(txtSalary) Then
  104.         MsgBox "Salary is not a valid amount."
  105.         ' Set focus on salary field, and
  106.         '   select all text.
  107.         With txtSalary
  108.             .SetFocus
  109.             .SelStart = 0
  110.             .SelLength = Len(.Text)
  111.         End With
  112.         Exit Sub
  113.     End If
  114.     With empNew
  115.         .ID = sbMain.NewEmployeeID
  116.         .Name = txtName.Text
  117.         .Salary = CDbl(txtSalary.Text)
  118.         sbMain.Employees.Add empNew, .ID
  119.         lstEmployees.AddItem .ID & ", " & .Name & ", " & .Salary
  120.         With lstEmployees
  121.             ' Select the newly added item.
  122.             .ListIndex = .NewIndex
  123.         End With
  124.     End With
  125.     txtName.Text = ""
  126.     txtSalary.Text = ""
  127.     txtName.SetFocus
  128. End Sub
  129. Private Sub cmdClose_Click()
  130.     Unload Me
  131. End Sub
  132. Private Sub cmdDeleteEmployee_Click()
  133.     Dim lngDeletedItem As Long
  134.     With lstEmployees
  135.         lngDeletedItem = .ListIndex
  136.         ' Check to make sure there is an employee selected.
  137.         If .ListIndex > -1 Then
  138.             ' The employee ID is the first six characters on the line.
  139.             sbMain.Employees.Remove Left(lstEmployees.Text, 6)
  140.             ' Remove the selected item.
  141.             .RemoveItem .ListIndex
  142.             If .ListCount = 0 Then
  143.                 ' If the list is now empty,
  144.                 '   don't attempt to set a new
  145.                 '   selection.
  146.                 Exit Sub
  147.             End If
  148.             ' Was the deleted item at the very bottom of
  149.             '   the list box?  If so, its index wil be
  150.             '   greater than or equal to the list count...
  151.             If .ListCount <= lngDeletedItem Then
  152.                 '   ...so set the current selection to
  153.                 '   the new bottom item...
  154.                 .ListIndex = lngDeletedItem - 1
  155.             Else
  156.                 '   ...otherwise, keep the selection in
  157.                 '   the same physical position in the
  158.                 '   list.
  159.                 .ListIndex = lngDeletedItem
  160.             End If
  161.         Else
  162.             MsgBox "No employee selected."
  163.         End If
  164.     End With
  165. End Sub
  166. Private Sub cmdListEmployees_Click()
  167.     Dim emp As Employee
  168.     With lstEmployees
  169.         .Clear
  170.         For Each emp In sbMain.Employees
  171.             .AddItem emp.ID & ", " & emp.Name & ", " & emp.Salary
  172.             ' After you press the Trouble button, clicking
  173.             '   Refresh causes a type mismatch error (either
  174.             '   in the For Each statement, if the invalid
  175.             '   item is the first one in the list, or at the
  176.             '   Next statement) when Visual Basic attempts
  177.             '   to put the reference to frmStraw into the
  178.             '   iteration variable emp.  To continue exe-
  179.             '   cution, drag the yellow execution arrow to
  180.             '   End Sub (or click on End Sub and then press
  181.             '   Ctrl+F9), then press F5.
  182.         Next
  183.         ' When you break here, see note above.
  184.         '
  185.         If .ListCount <> 0 Then
  186.             ' If there are any items in the list,
  187.             '   select the first.
  188.             .ListIndex = 0
  189.         End If
  190.     End With
  191. End Sub
  192. Private Sub cmdTrouble_Click()
  193.     ' Because the Collection object accepts
  194.     '   any object, a coding error can put
  195.     '   an invalid object in the collection.
  196.     sbMain.Employees.Add Me
  197.     MsgBox "A reference to the data entry form has just been added to the collection.  Press Refresh List to see the error this causes."
  198. End Sub
  199. Private Sub Form_Unload(Cancel As Integer)
  200.     ' Set all references to this form to
  201.     '   Nothing, to release its resources.
  202.     '   This means doing two things:
  203.     '   (1) Set the hidden global variable
  204.     '       the form to Nothing:
  205.     Set frmStraw = Nothing
  206.     '   (2) Clear the collection object,
  207.     '       because the Trouble button
  208.     '       put a reference to the form
  209.     '       into the collection -- creating
  210.     '       a circular reference (sbMain
  211.     '       has a reference to Employees,
  212.     '       which has a reference to the
  213.     '       form, which has a reference
  214.     '       to sbMain) that keeps all the
  215.     '       objects alive.
  216.     Set sbMain.Employees = Nothing
  217.     '
  218.     ' Of course, it's a bug that we can
  219.     '   destroy the SmallBusiness object's
  220.     '   Employees collection like this;
  221.     '   but House of Straw is the way
  222.     '   NOT to do things, after all.
  223. End Sub
  224. Private Sub txtName_Change()
  225.     Call EnableAddButton
  226. End Sub
  227. Private Sub txtSalary_Change()
  228.     Call EnableAddButton
  229. End Sub
  230. Private Sub txtSalary_KeyPress(KeyAscii As Integer)
  231.     Select Case KeyAscii
  232.         Case 48 To 57   ' Allow digits
  233.         Case 8      ' Allow backspace
  234.         Case 46     ' Allow period
  235.         Case Else
  236.             KeyAscii = 0
  237.             Beep
  238.     End Select
  239. End Sub
  240. Private Sub EnableAddButton()
  241.     If (Len(txtName) > 0) And (Len(txtSalary) > 0) Then
  242.         cmdAddEmployee.Enabled = True
  243.     Else
  244.         cmdAddEmployee.Enabled = False
  245.     End If
  246. End Sub
  247.